import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class RescueBoat here.
 * 
 * @author Andre de Zwart
 */
public class RescueBoat extends Actor
{
    private int prevX;
    private int prevY;
    private int victimCount = 0;
    private int speed = 5;
    
    GreenfootSound sound = new GreenfootSound("nautical010.wav");
    
    /**
     * Act - do whatever the RescueBoat wants to do. This method is called whenever
     * the 'Act' or 'Run' button gets pressed in the environment.
     */
    public void act() 
    {
        //Get current location
        prevX = getX();
        prevY = getY();
        
        //Move the player
        moveAround();

        //See if we hit a House
        if(hitHouse()){            
            //Find the hitted house
            House hittedHouse = (House)getOneIntersectingObject(House.class);
            
            if(isTouching(House.class)){
                this.setLocation(prevX, prevY);
            }
            
            if(victimCount < 3 && hittedHouse.getVictim()){              
                
                //Decrease the victim counter
                RescueBoats rb1 = (RescueBoats)getWorld();
                rb1.victimRescued();
                                
                //Increase rescued victims
                victimCount++;
                
                //Decrease the speed of the boat
                speed--;
                
                //Set another image to the player
                setBoatImage(victimCount);
                
                //Unset the victim
                hittedHouse.unsetVictim();                
            }  
            
        }
        
        //See if we hit the Quay
        if(hitQuay()){
            //Increase the world's victim count
            RescueBoats rb1 = (RescueBoats)getWorld();
            rb1.setRescuedCount(victimCount);
            
            //Unload the rescued victims.
            victimCount = 0;
            
            //Set the boat image
            setBoatImage(victimCount);
            
            //Reset the speed
            speed = 5;            
        }
        
        //See if we hit the Shore
        if(hitShore()){
            //Move player back to previous location
            if(prevY < this.getY()){
                setLocation(getX(),prevY);
            }
        }               
    } 
    
    /**
     * Move the player around
     */
    private void moveAround(){        
        
        if(Greenfoot.isKeyDown("up")){
            move(speed); 
            boatSound(true);
        }
        
        if(Greenfoot.isKeyDown("down")){
            move(-speed);
            boatSound(true);
        }        
        
        if(Greenfoot.isKeyDown("up") == false && Greenfoot.isKeyDown("down") == false){
            boatSound(false);
        }
        
        if(Greenfoot.isKeyDown("left")){
            turn(-4); 
        }
        
        if(Greenfoot.isKeyDown("right")){
            turn(4);
        }        
    }

    /**
     * Play/Stop the boat sounds
     */
    private void boatSound(boolean play){                
        if(play){
            if(!sound.isPlaying()){
                sound.play();
            } 
        }else{
            if(sound.isPlaying()){
                sound.stop();
            }
        }
    }
    
    /**
     * Detect hitting a House object
     */
    private boolean hitHouse(){
        Actor house = getOneIntersectingObject(House.class);
        
        if (house != null){
            return true;
        }else{
            return false;
        }
    }
    
    /**
     * Detect hitting a Quay object
     */
    private boolean hitQuay(){
        Actor quay = getOneIntersectingObject(Quay.class);
        
        if (quay != null){
            return true;
        }else{
            return false;
        }
    }
    
    /**
     * Detect hitting a Shore object
     */
    private boolean hitShore(){
        
        Actor boat = getOneIntersectingObject(Shore.class);
        
        if (boat != null){
            return true;
        }else{
            return false;
        }
        
    }
    
    /**
     * Set the right image for the boats victimCount
     */
    private void setBoatImage(int Cnt){

        switch(Cnt){
            case 0:
                this.setImage("RIB0.png");
                break;
            case 1:
                this.setImage("RIB1.png");
                break;
            case 2:
                this.setImage("RIB2.png");
                break;
            case 3:
                this.setImage("RIB3.png");  
                break;
        } 
        
    }
    
}
